package quick.start.ui.widget;

import quick.start.ui.R;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.FrameLayout;

public class DropShadowFrameLayout extends FrameLayout {

	private static final int LOC_LEFT = 0;
	private static final int LOC_TOP = 1;
	private static final int LOC_RIGHT = 2;
	private static final int LOC_BOTTOM = 3;
	
	private int dropShadowRightId;
	private int dropShadowLeftId;
	private int dropShadowTopId;
	private int dropShadowBottomId;
	
	private boolean dropShadowRight;
	private boolean dropShadowLeft;
	private boolean dropShadowTop;
	private boolean dropShadowBottom;
	
	private Canvas canvas;
	
	public DropShadowFrameLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		
        TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.DropShadowListView);
        dropShadowRightId = a.getResourceId(R.styleable.DropShadowListView_dropShadowRightSrc, -1);
        dropShadowLeftId = a.getResourceId(R.styleable.DropShadowListView_dropShadowLeftSrc, -1);
        dropShadowTopId = a.getResourceId(R.styleable.DropShadowListView_dropShadowTopSrc, -1);
        dropShadowTopId = a.getResourceId(R.styleable.DropShadowListView_dropShadowBottomSrc, -1);

        dropShadowRight = a.getBoolean(R.styleable.DropShadowListView_dropShadowRight, false);
        dropShadowLeft = a.getBoolean(R.styleable.DropShadowListView_dropShadowLeft, false);
        dropShadowTop = a.getBoolean(R.styleable.DropShadowListView_dropShadowTop, false);
        dropShadowBottom = a.getBoolean(R.styleable.DropShadowListView_dropShadowBottom, false);

        a.recycle();
	}

	@Override
	protected void dispatchDraw(Canvas canvas){
		super.dispatchDraw(canvas);
		this.canvas = canvas;
		dropShadow(canvas);
	}
	
	public void clearShadow(){
		clearShadow(this.canvas);
	}
	
	private void dropShadow(Canvas canvas){
        Bitmap bm;
        if (dropShadowRight) {
            bm = getBitmap(dropShadowRightId);
            canvas.drawBitmap(bm, null, getDropShadowArea(LOC_RIGHT, canvas, bm), null);
        }

        if (dropShadowLeft) {
            bm = getBitmap(dropShadowLeftId);
            canvas.drawBitmap(bm, null, getDropShadowArea(LOC_LEFT, canvas, bm), null);
        }

        if (dropShadowTop) {
            bm = getBitmap(dropShadowTopId);
            canvas.drawBitmap(bm, null, getDropShadowArea(LOC_TOP, canvas, bm), null);
        }

        if (dropShadowBottom) {
            bm = getBitmap(dropShadowBottomId);
            canvas.drawBitmap(bm, null, getDropShadowArea(LOC_BOTTOM, canvas, bm), null);
        }
	}
	
	private void clearShadow(Canvas canvas){
        Bitmap bm;
        if (dropShadowRight) {
            bm = getBitmap(dropShadowRightId);
            canvas.drawBitmap(null, null, getDropShadowArea(LOC_RIGHT, canvas, bm), null);
        }

        if (dropShadowLeft) {
            bm = getBitmap(dropShadowLeftId);
            canvas.drawBitmap(null, null, getDropShadowArea(LOC_LEFT, canvas, bm), null);
        }

        if (dropShadowTop) {
            bm = getBitmap(dropShadowTopId);
            canvas.drawBitmap(null, null, getDropShadowArea(LOC_TOP, canvas, bm), null);
        }

        if (dropShadowBottom) {
            bm = getBitmap(dropShadowBottomId);
            canvas.drawBitmap(null, null, getDropShadowArea(LOC_BOTTOM, canvas, bm), null);
        }
	}
	
	private Bitmap getBitmap(int id){
		return BitmapFactory.decodeResource(getContext().getResources(), id);
	}
	
	private Rect getDropShadowArea(int loc, Canvas canvas, Bitmap bitmap){
        Rect rect = canvas.getClipBounds();
        switch (loc) {
        case LOC_LEFT:
            return new Rect(0, 0, bitmap.getWidth(), canvas.getHeight());
        case LOC_TOP:
            return new Rect(0, 0, rect.right, bitmap.getHeight());
        case LOC_RIGHT:
            return new Rect(rect.right - bitmap.getWidth(), 0, rect.right, rect.bottom);
        case LOC_BOTTOM:
            return new Rect(0, rect.bottom - bitmap.getHeight(), rect.right, rect.bottom);
        default:
            return null;
        }
	}
}
